﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetDxComponents.ExtenderProviders.Settings;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using ProgNetDxComponents.ExtenderProviders.Layout;
using System.ComponentModel;

namespace ProgNetDxComponents.ExtenderProviders
{
    public class LayoutProvider : ProgNetComponents.ExtenderProviders.BaseExtenderProvider<LayoutSettings>
    {
        LayoutHelper helper = new LayoutHelper();

        public string LayoutName
        {
            get { return helper.LayoutName; }
            set { helper.LayoutName = value; }
        }
        public bool LayoutPerUser
        {
            get { return helper.LayoutPerUser; }
            set { helper.LayoutPerUser = value; }
        }
        public string UserName
        {
            get { return helper.UserName; }
            set { helper.UserName = value; }
        }
        public string DefaultLayout
        {
            get { return helper.DefaultLayout; }
        }

        protected override void ProcessObject(object sender, LayoutSettings settings)
        {
            if (settings.SaveLayout)
            {
                if (sender is ColumnView)
                    helper.Add(sender as ColumnView, settings.GridCtrlLayoutProperties);

                if (sender is SplitContainerControl)
                    helper.Add(sender as SplitContainerControl, settings.SplitContainerLayoutProperties);

                if (sender is TreeList)
                    helper.Add(sender as TreeList, settings.TreeListLayoutProperties);
            }
            else
            {
                if (sender is ColumnView)
                    helper.Remove(sender as ColumnView);

                if (sender is SplitContainerControl)
                    helper.Remove(sender as SplitContainerControl);

                if (sender is TreeList)
                    helper.Remove(sender as TreeList);
            }
        }

        public override bool CanExtend(object extendee)
        {
            if (extendee is ColumnView ||
                extendee is SplitContainerControl ||
                extendee is TreeList)
                return true;
            return false;
        }

        public bool SaveLayout()
        {
            return LayoutControllerBase.Instance.SaveCurrentLayout(helper);
        }
        public bool LoadSavedLayout()
        {
            return LayoutControllerBase.Instance.ApplaySavedLayout(helper);
        }
        public bool RestoreDefaultLayout()
        {
            return LayoutControllerBase.Instance.RestoreDefaultLayout(helper);
        }
        public override void EndInit()
        {
            base.EndInit();
            SetCurrentLayoutAsDefault();
        }
        public void SetCurrentLayoutAsDefault()
        {
            helper.SetCurrentLayoutAsDefault();
        }
    }
}
